import React from "react";

export const AuthContext = React.createContext();  // 登录上下文环境

export const authInitialState = {
    isAuthenticated: false,
    user: null,
    token: null  // 每次请求的时候都会带上
}

export const authReducer = (state, action) => {
    switch(action.type) {
        case "LOGIN":
            // localStorage 存储  属于h5 
            localStorage.setItem("user", JSON.stringify(action.payload.user))
            localStorage.setItem("token", JSON.stringify(action.payload.token))
            return { 
                ...state,
                isAuthenticated: true,
                user: action.payload.user,
                token: action.payload.token  // 令牌
            }
        case "LOGOUT":
            return {
                ...state,
                isAuthenticated: false,
                user: null
            }
        default:
            return state
    }
}